package com.hxut.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxut.Controller.Utils.Result;
import com.hxut.domain.Book;
import com.hxut.service.BookService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
@Api(tags = "首页模块")
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @ApiOperation(value = "查询全部图书",notes = "查询所有的图书信息")
    @ApiResponses({
            @ApiResponse(code=100,message = "查询成功"),
            @ApiResponse(code=200,message = "查询成功")
    })
    @GetMapping
    public Result getAll(){
        return  new Result(true,bookService.getAll());
    }

    @PostMapping
    @ApiOperation(value ="保存图书",notes = "保存成功返回flag")
    @ApiImplicitParam(name = "book",value = "传入图书信息",required = true)
    public Result save(@RequestBody Book book) throws IOException {
        /*Result result = new Result();
        result.setFlag(bookService.save(book));
        result.setData(null);
        return result;*/
        if(book.getName().equals("123")) throw new IOException();
        Boolean flag = bookService.save(book);
        return new Result(flag,flag?"添加成功^_^":"添加失败-_-");
    }

    @ApiOperation(value = "删除图书",notes = "根据id删除图书")
    @ApiImplicitParam(name="id",value = "图书id",required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return new Result(bookService.delete(id));
    }

    @PutMapping
    @ApiOperation(value = "编辑图书",notes = "根据传入的数据更新图书信息")
    @ApiImplicitParam(name="book",value = "图书信息",required = true)
    public Result update(@RequestBody Book book){
       return new Result(bookService.update(book));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取图书",notes = "根据id获取图书")
    @ApiImplicitParam(name="id",value = "图书id",required = true)
    public Result getById(@PathVariable Integer id){
        return new Result(true,bookService.getById(id));
    }

   /* @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable  Integer currentPage,@PathVariable Integer pageSize){
        IPage<Book> Ipage = bookService.getByPage(currentPage, pageSize);
        //如果当前页码值大于总的页码值，那么重新执行查询，把最大页码值作为当前页码值
        if(currentPage>Ipage.getPages()){
            Ipage=bookService.getByPage((int)Ipage.getPages(),pageSize);
        }
        return new Result(true,Ipage);
    }*/

    @ApiOperation(value = "分页操作",notes = "MybatisPlus实现分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name="currentPage",value="当前页",required = true),
            @ApiImplicitParam(name="pageSize",value = "分页数",required = true)
    })
    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable  Integer currentPage,@PathVariable Integer pageSize,String name,Book book){
        System.out.println("参数1 ->"+name);
        System.out.println("参数2 ->"+book);
        IPage<Book> Ipage = bookService.getByPage(currentPage, pageSize,book);
        //如果当前页码值大于总的页码值，那么重新执行查询，把最大页码值作为当前页码值
        if(currentPage>Ipage.getPages()){
            Ipage=bookService.getByPage((int)Ipage.getPages(),pageSize,book);
        }
        return new Result(true,Ipage);
    }

}
